package cn.itcast.bassm.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import cn.itcast.bassm.service.TestInterfaceService;
@Service
public class TestInterfaceServiceImpl implements TestInterfaceService {
	
	public  String doPostSelect(String urlStr,String idnumber) {
        String result = null;
        HttpPost httpPost = new HttpPost(urlStr);
    	List<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("idnumber",idnumber));

        try {

        	HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
            httpPost.setEntity(entity);// 设置参数实体
            @SuppressWarnings("resource")
            HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时
            HttpResponse httpResp = httpClient.execute(httpPost);// 获取HttpResponse实例
            if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
            } else {
                result="100";
            }
        } catch (ConnectTimeoutException e) {
        	result="100";
        } catch (UnsupportedEncodingException e) {
        	result="100";
        } catch (IOException e) {
        	result="100";
        } catch (Exception e) {
        	result="100";
        }

        return result;
    }
    
    
    public  String doPostSelect2(String urlStr,String idnumber,String starttime) {
        String result = null;
        HttpPost httpPost = new HttpPost(urlStr);
    	List<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("idnumber",idnumber));
    	params.add(new BasicNameValuePair("starttime",starttime));

        try {

        	HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
            httpPost.setEntity(entity);// 设置参数实体
            @SuppressWarnings("resource")
            HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时
            HttpResponse httpResp = httpClient.execute(httpPost);// 获取HttpResponse实例
            if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
            } else {
                result="100";
            }
        } catch (ConnectTimeoutException e) {
        	result="100";
        } catch (UnsupportedEncodingException e) {
        	result="100";
        } catch (IOException e) {
        	result="100";
        } catch (Exception e) {
        	result="100";
        }

        return result;
    }
    
    
    public  String doPostSelect3(String urlStr,String idnumber,String datetime,String doortype) {
        String result = null;
        HttpPost httpPost = new HttpPost(urlStr);
    	List<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("id","1"));
    	params.add(new BasicNameValuePair("idnumber",idnumber));
    	params.add(new BasicNameValuePair("datetime",datetime));
    	params.add(new BasicNameValuePair("doortype",doortype));

        try {

        	HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
            httpPost.setEntity(entity);// 设置参数实体
            @SuppressWarnings("resource")
            HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时
            HttpResponse httpResp = httpClient.execute(httpPost);// 获取HttpResponse实例
            if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
            } else {
                result="100";
            }
        } catch (ConnectTimeoutException e) {
        	result="100";
        } catch (UnsupportedEncodingException e) {
        	result="100";
        } catch (IOException e) {
        	result="100";
        } catch (Exception e) {
        	result="100";
        }

        return result;
    }
    
    
    public  String doPostSelect4(String urlStr,String pad,String failtype,String usertype) {
        String result = null;
        HttpPost httpPost = new HttpPost(urlStr);
    	List<NameValuePair> params = new ArrayList<NameValuePair>();
    	params.add(new BasicNameValuePair("id","1"));
    	params.add(new BasicNameValuePair("pad",pad));
    	params.add(new BasicNameValuePair("failtype",failtype));
    	params.add(new BasicNameValuePair("usertype",usertype));

        try {

        	HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
            httpPost.setEntity(entity);// 设置参数实体
            @SuppressWarnings("resource")
            HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时
            HttpResponse httpResp = httpClient.execute(httpPost);// 获取HttpResponse实例
            if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
            } else {
                result="100";
            }
        } catch (ConnectTimeoutException e) {
        	result="100";
        } catch (UnsupportedEncodingException e) {
        	result="100";
        } catch (IOException e) {
        	result="100";
        } catch (Exception e) {
        	result="100";
        }

        return result;
    }

}
